//
//  BaseAnimationView.m
//  project
//
//  Created by 黄建华 on 2018/6/26.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "BaseAnimationView.h"
#define BaseAnimationduration 1.0
@implementation BaseAnimationView
DEF_SINGLETON(BaseAnimationView)
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    }
    
    return self;
}

// 位置移动
- (void)positionAnimation:(UIView *)view fromSize:(CGPoint)size1 toSize:(CGPoint)size2 {
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
    
    anima.fromValue = [NSValue valueWithCGPoint:size1];
    anima.toValue   = [NSValue valueWithCGPoint:size2];
    anima.duration  = BaseAnimationduration;
    //    anima.repeatCount = 100; 重复次数
    //如果fillMode=kCAFillModeForwards和removedOnComletion=NO，那么在动画执行完毕后，图层会保持显示动画执行后的状态。但在实质上，图层的属性值还是动画执行前的初始值，并没有真正被改变。
    //anima.fillMode = kCAFillModeForwards;
    //anima.removedOnCompletion = NO;
    anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [view.layer addAnimation:anima forKey:@"positionAnimation"];
}

// 透明度变化
- (void)opacityAniamtion:(UIView *)view fromAlpha:(CGFloat)alpha1 toAlpha:(CGFloat)alpha2 {
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    
    anima.fromValue = [NSNumber numberWithFloat:alpha1];
    anima.toValue   = [NSNumber numberWithFloat:alpha2];
    anima.duration  = BaseAnimationduration;
    [view.layer addAnimation:anima forKey:@"opacityAniamtion"];
}

// 缩放
- (void)scaleAnimation:(UIView *)view scale:(CGFloat)size {
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    anima.toValue  = [NSNumber numberWithFloat:size];
    anima.duration = BaseAnimationduration;
    anima.autoreverses = NO;
    [view.layer addAnimation:anima forKey:@"scaleAnimation"];
}

// 旋转
- (void)rotateAnimation:(UIView *)view rotate:(CGFloat)value {
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//绕着z轴为矢量，进行旋转(@"transform.rotation.z"==@@"transform.rotation")
    
    anima.toValue  = [NSNumber numberWithFloat:value];
    anima.duration = 1.0f;
    [view.layer addAnimation:anima forKey:@"rotateAnimation"];
}

// 关键帧动画
- (void)keyFrameAnimation {
    CAKeyframeAnimation *anima  = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSValue             *value0 = [NSValue valueWithCGPoint:CGPointMake(0, 100)];
    NSValue             *value1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    
    anima.values         = [NSArray arrayWithObjects:value0, value1, nil];
    anima.duration       = 2.0f;
    anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];//设置动画的节奏
    anima.delegate       = self;//设置代理，可以检测动画的开始和结束
    //    [_demoView.layer addAnimation:anima forKey:@"keyFrameAnimation"];
}

// 路径动画
- (void)pathAnimation {
    CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath        *path  = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 100, 200, 200)];
    
    anima.path     = path.CGPath;
    anima.duration = 2.0f;
    //    [_demoView.layer addAnimation:anima forKey:@"pathAnimation"];
}

- (void)shakeAnimation {
    CAKeyframeAnimation *anima  = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];//在这里@"transform.rotation"==@"transform.rotation.z"
    NSValue             *value1 = [NSNumber numberWithFloat:-M_PI/180*4];
    NSValue             *value2 = [NSNumber numberWithFloat:M_PI/180*4];
    NSValue             *value3 = [NSNumber numberWithFloat:-M_PI/180*4];
    
    anima.values      = @[value1, value2, value3];
    anima.repeatCount = MAXFLOAT;
    //    [_demoView.layer addAnimation:anima forKey:@"shakeAnimation"];
}

@end
